package org.zhxy.core.system.controller;

import com.baidu.aip.util.Base64Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.zhxy.common.response.FaceLoginResult;
import org.zhxy.common.response.QRCode;
import org.zhxy.common.response.Result;
import org.zhxy.common.response.ResultCode;
import org.zhxy.common.utils.BaiduAiUtil;
import org.zhxy.core.system.service.FaceLoginService;

@RestController
@RequestMapping("/access/faceLogin")
public class FaceLoginController {

    @Autowired
    private BaiduAiUtil baiduAiUtil;

    @Autowired
    private FaceLoginService faceLoginService;


    @RequestMapping(value = "/qrcode", method = RequestMethod.GET)
    public Result qrcode() throws Exception {
        QRCode qrCode = faceLoginService.getQRCode();
        System.out.println(qrCode);
        return new Result(ResultCode.SUCCESS_NO_MESSAGE,qrCode);
    }

    @RequestMapping(value = "/qrcode/{code}", method = RequestMethod.GET)
    public Result qrcodeCeck(@PathVariable(name = "code") String code) throws Exception {
        FaceLoginResult result = faceLoginService.checkQRCode(code);
        return new Result(ResultCode.SUCCESS_NO_MESSAGE,result);
    }

    @RequestMapping(value = "/checkFace", method = RequestMethod.POST)
    public Result checkFace(@RequestParam(name = "file") MultipartFile attachment) throws Exception {
        String image = Base64Util.encode(attachment.getBytes());
        Boolean aBoolean = baiduAiUtil.faceCheck(image);
        if(aBoolean){
            return new Result(ResultCode.SUCCESS_NO_MESSAGE);
        }
        return Result.FAIL();
    }

    @RequestMapping(value = "/{code}", method = RequestMethod.POST)
    public Result loginByFace(@PathVariable(name = "code") String code, @RequestParam(name = "file") MultipartFile attachment) throws Exception {
        String token = faceLoginService.loginByFace(code, attachment);
        if(token != null){
            return new Result(ResultCode.SUCCESS_NO_MESSAGE,token);
        }
        return new Result(ResultCode.MOBILE_OR_PASSWORD_OR_TYPE_ERROR);
    }

}
